//
//  LGTabBar.m
//  微博
//
//  Created by MS on 15-12-3.
//  Copyright (c) 2015年 MS. All rights reserved.
//

#import "LGTabBar.h"
#import "LGTabBarButton.h"

@interface LGTabBar ()

@property (nonatomic,weak) UIButton * plusBtn;

@property (nonatomic,strong) NSMutableArray * buttons;

@property (nonatomic,weak) UIButton * selectButton;

@end

@implementation LGTabBar

-(NSMutableArray *)buttons
{
    if (!_buttons) {
        
        _buttons = [NSMutableArray array];
    }
    return _buttons;
}


-(void)setItems:(NSArray *)items
{
    _items = items;
    
    for (UITabBarItem * item in items) {
        
        LGTabBarButton * btn =[LGTabBarButton buttonWithType:UIButtonTypeCustom];
        btn.item = item;
        
        btn.tag = self.buttons.count;
        
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchDown];
        
        if (btn.tag ==0) {//默认选中第一个
            [self btnClick:btn];
        }
        
        [self addSubview:btn];
        
        [self.buttons addObject:btn];
    }
    
}

-(void)selectBtn:(NSInteger) index
{
    for (LGTabBarButton *  btn in self.buttons) {
        if (btn.tag==index) {
            _selectButton.selected = NO;
            btn.selected = YES;
            _selectButton = btn;
            if ([self.delegate respondsToSelector:@selector(tabBar:didClickButton:)]) {
                
                [self.delegate tabBar:self didClickButton:btn.tag];
            }

        }
        
    }
    
}


-(void) btnClick:(UIButton *)button
{
    _selectButton.selected = NO;
    button.selected = YES;
    _selectButton = button;
    
    if ([self.delegate respondsToSelector:@selector(tabBar:didClickButton:)]) {
        
        [self.delegate tabBar:self didClickButton:button.tag];
    }
    
}

-(UIButton *)plusBtn
{
    if (!_plusBtn) {
        
        UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add"] forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add_highlighted"] forState:UIControlStateHighlighted];
        [btn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button"] forState:UIControlStateNormal];
        [btn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button_highlighted"] forState:UIControlStateHighlighted];
        
        //根据图片自动设置大小和图片一样
        [btn sizeToFit];
        [btn addTarget:self action:@selector(pluseClick) forControlEvents:UIControlEventTouchUpInside];
        _plusBtn =btn;
        
        [self addSubview:_plusBtn];
    }
    return _plusBtn;
    
}

//点击加号按钮调用
-(void)pluseClick
{
    if ([_delegate respondsToSelector:@selector(tabBardidClickPluseButton:)]) {
        
        [_delegate tabBardidClickPluseButton:self];
    }
    
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    
    CGFloat W = self.bounds.size.width;
    CGFloat H = self.bounds.size.height;
    
    CGFloat btnY = 0;
    CGFloat btnW =W/(self.items.count+1);
    CGFloat btnH =self.bounds.size.height;
    
    int i = 0;
    //设置tabBarButton的frame
    for (UIView * tabBarButton in self.buttons) {
        
            if (i==2) {
                
                i=3;
            }
            
            CGFloat btnX = btnW*i;
            tabBarButton.frame = CGRectMake(btnX, btnY, btnW, btnH);
        
            i++;
    }
    
    
    self.plusBtn.center = CGPointMake(W*0.5, H*0.5);
    
}

@end
